package cn.px.service.repair.impl;

import com.github.pagehelper.PageHelper;
import cn.px.common.bean.AbstractPageResultBean;
import cn.px.dao.bean.MyPage;
import cn.px.dao.dto.repair.ErrorTypeDTO;
import cn.px.dao.entity.repair.ErrorType;
import cn.px.dao.mapper.repair.ErrorTypeMapper;
import cn.px.service.repair.ErrorTypeService;
import cn.px.service.util.ExecuteSql;
import org.springframework.stereotype.Service;
import cn.px.service.base.impl.BaseServiceImpl;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;
import lombok.AllArgsConstructor;

import java.util.List;

/**
 * @Description 故障类别 service 实现类
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2019,北京乾元坤和科技有限公司 All rights reserved.
 */
@AllArgsConstructor
@Service("errorTypeService")
public class ErrorTypeServiceImpl extends BaseServiceImpl<ErrorTypeMapper, ErrorType, ErrorTypeDTO> implements ErrorTypeService {

    @Override
    public int batchDelete(List<String> ids) {
        Weekend<ErrorType> weekend = new Weekend<>(ErrorType.class);
        WeekendCriteria<ErrorType, Object> weekendCriteria = weekend.weekendCriteria();
        weekendCriteria.andIn(ErrorType::getId, ids);
        mapper.deleteByExample(weekend);
        return 1;
    }

    @Override
    public AbstractPageResultBean<ErrorType> selectPageInfo(ErrorTypeDTO record, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        AbstractPageResultBean<ErrorType> pageInfo = new MyPage(mapper.selectPageList(record));
        return pageInfo;
    }

    @Override
    public ErrorType getModelById(Object id) {
        ErrorType model = mapper.selectById(id);
        return model;
    }
    @Override
    public List<ErrorType> getModelListByDTO(ErrorTypeDTO model) {
        Weekend<ErrorType> weekend = new Weekend<>(ErrorType.class);
        setWeekendCriteria(weekend,model);
        List<ErrorType> result = mapper.selectByExample(weekend);
        return result;
    }

    /**
     * 设置查询条件
     */
    private void setWeekendCriteria(Weekend<ErrorType> weekend,ErrorTypeDTO model) {
        WeekendCriteria<ErrorType, Object> weekendCriteria = weekend.weekendCriteria();
        ExecuteSql.executeNotEmpty(model.getId(), () -> weekendCriteria.andNotEqualTo(ErrorType::getId,model.getId()));
    }

    @Override
    public ErrorType saveModelByDTO(ErrorTypeDTO model) {
        return super.saveModelByDTO(model);
    }
}
